<?php

namespace App\Http\Controllers\User;

use App\Http\Classes\UserHard;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class HardController extends Controller
{
    //
    /*
     * 添加一个单词到用户生词本
     * POST
     * word (int)
     */
    public function addHardWord(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['word']) )
            {
                $dataArray = [];
                $dataArray['word_id'] = $data['word'];
                $dataArray['user_id'] = $_COOKIE['USER_ID'];
                $dataArray['type'] = 1;
                $hard = new UserHard();
                if ( $hard->addHardWord($dataArray) )
                {
                    return $this->response(1, '添加成功', []);
                }
                else
                {
                    return $this->response(0, '添加失败', [], $hard->getMessage() );
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 添加多个单词到用户生词本
     * POST
     * words (array)
     */
    public function addHardWords(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['words']) && is_array($data['words']) )
            {
                $dataArray = [];
                $words = $data['words'];
                for ($i = 0; $i < count($words); $i++)
                {
                    $dataArray[$i]['word_id'] = $words[$i];
                    $dataArray[$i]['user_id'] = $_COOKIE['USER_ID'];
                    $dataArray[$i]['type'] = 1;
                }

                $hard = new UserHard();
                if ( $hard->addHardWord($dataArray) )
                {
                    return $this->response(1, '添加成功', []);
                }
                else
                {
                    return $this->response(0, '添加失败', [], $hard->getMessage() );
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户生词本
     * GET
     */
    public function getHardWords()
    {
        try
        {
            $hard = new UserHard();

            $hards = $hard->getUserHardWords($_COOKIE['USER_ID']);

            if ( $hards )
            {
                return $this->response(1, '获取成功', $hards);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $hard->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 从用户生词本内删除一条生词
     * POST
     * hard
     */
    public function delHardWord(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['hard']) )
            {
                $hard = new UserHard( $data['hard'] );

                if ( $hard->delWord($_COOKIE['USER_ID']) )
                {
                    return $this->response(1, '删除成功', []);
                }
                else
                {
                    return $this->response(0, '删除失败', [], $hard->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 从用户生词本内删除多条生词
     * POST
     * hards (array)
     */
    public function delHardWords(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['hards']) && is_array($data['hards']) )
            {
                $hard = new UserHard();

                if ( $hard->delWords($data['hards'], $_COOKIE['USER_ID']) )
                {
                    return $this->response(1, '删除成功', []);
                }
                else
                {
                    return $this->response(0, '删除失败', [], $hard->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数或参数不规范', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 添加一条生句到用户生句库
     * POST
     * sentence （int）
     */
    public function addHardSentence(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['sentence']) )
            {
                $dataArray = [];
                $dataArray['sentence_id'] = $data['sentence'];
                $dataArray['user_id'] = $_COOKIE['USER_ID'];
                $dataArray['type'] = 2;
                $hard = new UserHard();
                if ( $hard->addHardSentence($dataArray) )
                {
                    return $this->response(1, '添加成功', []);
                }
                else
                {
                    return $this->response(0, '添加失败', [], $hard->getMessage() );
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 添加多个单词到用户生词本
     * POST
     * sentences (array)
    */
    public function addHardSentences(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['sentences']) && is_array($data['sentences']) )
            {
                $dataArray = [];
                $sentences = $data['sentences'];
                for ($i = 0; $i < count($sentences); $i++)
                {
                    $dataArray[$i]['sentence_id'] = $sentences[$i];
                    $dataArray[$i]['user_id'] = $_COOKIE['USER_ID'];
                    $dataArray[$i]['type'] = 2;
                }

                $hard = new UserHard();
                if ( $hard->addHardSentence($dataArray) )
                {
                    return $this->response(1, '添加成功', []);
                }
                else
                {
                    return $this->response(0, '添加失败', [], $hard->getMessage() );
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    public function getHardSentences()
    {
        try
        {
            $hard = new UserHard();

            $hards = $hard->getUserHardSentences($_COOKIE['USER_ID']);

            if ( $hards )
            {
                return $this->response(1, '获取成功', $hards);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $hard->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 从用户生句库删除一条生句
     * POST
     * hard(int)
     */
    public function delHardSentence(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['hard']) )
            {
                $hard = new UserHard( $data['hard'] );

                if ( $hard->delSentence($_COOKIE['USER_ID']) )
                {
                    return $this->response(1, '删除成功', []);
                }
                else
                {
                    return $this->response(0, '删除失败', [], $hard->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 从用户生句库内删除多条生句
     * POST
     * hards (array)
     */
    public function delHardSentences(Request $request)
    {
        try
        {
            $data = $request->all();

            if ( isset($data['hards']) && is_array($data['hards']) )
            {
                $hard = new UserHard();

                if ( $hard->delSentences($data['hards'], $_COOKIE['USER_ID']) )
                {
                    return $this->response(1, '删除成功', []);
                }
                else
                {
                    return $this->response(0, '删除失败', [], $hard->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数或参数不规范', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
